import axios from 'axios'
import type { InternalAxiosRequestConfig } from 'axios'

const service = axios.create({
  baseURL: (import.meta as any).env.VITE_API_BASE_URL,
  timeout: 5000
})

service.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers = config.headers || {}
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

service.interceptors.response.use(
  response => response.data,
  error => Promise.reject(error)
)

function request<T = any>(config: any): Promise<T> {
  return service(config)
}

export default request
